package com.dupf.upms.client.shiro.filter;

import com.dupf.common.util.RedisUtil;
import com.dupf.upms.client.shiro.session.UpmsSessionDao;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * 重写Logout过滤器
 * Created by dupf on 2018/3/1.
 */
public class UpmsLogoutFilter extends LogoutFilter {

    @Autowired
    UpmsSessionDao upmsSessionDao;

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {

        try {
            Subject subject = getSubject(request, response);
            RedisUtil.remove("user:info:redis:prefix:" + subject.getPrincipal());
            Session session = subject.getSession();
            upmsSessionDao.delete(session);
            subject.logout();
        } catch (Exception e){

        }
        return false;

    }
}
